<?php
namespace come\log;
/**
 * 文件日志类
 * Class File
 * @package come\log
 */
class File
{
    /**
     * 文件日志记录
     * @param $content
     * @param string $type_name 日志类型
     * @param bool $trace 是否记录堆栈信息
     */
    static function addLog($content, $type_name='error', $trace=true){
        if(!is_dir(LOG_PATH)){
            mkdir(RUNTIME_PATH);
            mkdir(LOG_PATH);
        }

        if(is_array($content)){
            $content = json_encode($content,JSON_UNESCAPED_UNICODE);
        }

        $fileName = LOG_PATH.$type_name.date("Ymd").'.log';

        error_log('['.date('Y-m-d H:i:s').'] '.$content."\r\n",3,$fileName);

        if($trace) {
            //打印堆栈信息
            $traceArr = debug_backtrace();
            unset($traceArr[0]);
            $traceInfo = '';
            foreach($traceArr as $key=>$value) {
                if(strpos($value['file'],"public/index.php")
                    || strpos($value['file'],"come/App.php")
                    || strpos($value['file'],"come/Request.php")){
                    continue;
                }
                $traceInfo .= $value['file'].':'.$value['line'].'行，调用方法:'.$value['function']."\r\n";
            }
            error_log('['.date('Y-m-d H:i:s').'] '.'堆栈信息：'."\r\n".$traceInfo."\r\n\r\n",3,$fileName);
        }
    }
}